package com.doudoukeji.service.Activity.my.edituser.edituserinf;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.doudoukeji.service.Activity.my.edituser.EditUserInfoPresenter;
import com.doudoukeji.service.Activity.my.edituser.EditUserInfoView;
import com.doudoukeji.service.Bean.ServiceLitapal;
import com.doudoukeji.service.Bean.Token;
import com.doudoukeji.service.R;
import com.sanhai.android.adapter.CommonAdapter;
import com.sanhai.android.adapter.ViewHolder;
import com.sanhai.android.mvp.EduActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述：  服务项目
 * @项目名称：ddgh
 * @类名称： EditUserInfActivity
 * @包名称： com.doudoukeji.service.Activity.my.edituser.edituserinf
 * @创建人： zhanjunjie
 * @创建时间：2016/3/10 15:45
 * @修改人：
 * @修改时间：2016/3/10 15:45
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class EditUserInfActivity extends EduActivity implements EditUserInfoView {

    //标题
    private TextView mTvTitle;

    //Listview
    private ListView listView;
    private EditUserInfAdapter adapter;
    private List<ServiceLitapal> litapals = new ArrayList<ServiceLitapal>();
    private EditUserInfoPresenter presenter;

    //获取我的服务
    private List<String> strings = new ArrayList<String>();
    private String serviceid = "";
    private StringBuffer mSbId = new StringBuffer();

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_edituserinf);
    }

    @Override
    public void init() {
        // 设置标题
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setText("服务项目");
        findViewById(R.id.btn_submit).setOnClickListener(this);
        litapals = ServiceLitapal.getAllServiceLitapal();
        if (!"".equals(Token.getServices())) {
            String[] str = Token.getServices().split(",");
            for (int i = 0; i < str.length; i++) {
                strings.add(str[i]);
            }
        }
        listView = (ListView) findViewById(R.id.listview);
        adapter = new EditUserInfAdapter(this, litapals, strings);
        listView.setAdapter(adapter);

        presenter = new EditUserInfoPresenter(this, this);

    }

    @Override
    public void onClickEvent(View view) {

        for (int i = 0; i < adapter.getCount(); i++) {
            CheckBox mcb = (CheckBox) listView.getChildAt(i).findViewById(R.id.item_cb);
            if (mcb.isChecked()) {
                mSbId.append(adapter.getItem(i).getServiceid() + ",");
            }
        }
        if (!"".equals(mSbId) && mSbId.length() > 0) {
            serviceid = mSbId.substring(0, mSbId.length() - 1);
        } else {
            showToastMessage("请至少选择一种服务项目");
            return;
        }
        presenter.updateUserInfo("", "", "", "", serviceid);
    }




    @Override
    public void updateSucced() {
        showToastMessage("选择服务成功");
        Token.setServices(serviceid);
        this.finish();
    }

    @Override
    public void updateFail() {
        showToastMessage("修改失败");
    }
}
